package cn.yx.common.core.util;

import cn.hutool.core.codec.Base64;

/**
 * <p>字节数组编码转换工具类</p>
 *
 * @author Wgssmart
 */
public final class ByteUtil {

    private static final char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

    private ByteUtil() {
    }

    /**
     * 字节数组以十六进制的形式编码成字符串
     *
     * @param bytes 字节数组
     * @return 十六进制编码后的字符串
     */
    public static String toHex(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        int index = 0;
        for (byte b : bytes) {
            hexChars[index++] = HEX_CHAR[b >>> 4 & 0xF];
            hexChars[index++] = HEX_CHAR[b & 0xF];
        }
        return new String(hexChars);
    }

    /**
     * 字节数组以Base64的形式编码成字符串
     *
     * @param bytes 字节数组
     * @return base64编码后的字符串
     */
    public static String toBase64(byte[] bytes) {
        return Base64.encode(bytes);
    }

}
